package com.csii.autocode.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

/**
 * 读取配置文件
 */
public class ReadConfig {
    private static final Logger logger = LoggerFactory.getLogger(ReadConfig.class);

    private static final String databaseUrl = "auto-code/autocode_database";

    private static final String configUrl = "auto-code/autocode_config";

    private static ResourceBundle database = getConfigByName(databaseUrl);

    static {
        if (database == null) {
            logger.debug("通过class目录加载配置文件 " + databaseUrl);
            database = ResourceBundle.getBundle(databaseUrl);
        }
    }

    private static ResourceBundle config = getConfigByName(configUrl);

    static {
        if (config == null) {
            logger.debug("通过class目录加载配置文件 " + configUrl);
            config = ResourceBundle.getBundle(configUrl);
        }
    }

    private static ResourceBundle getConfigByName(String paramString) {
        PropertyResourceBundle propertyResourceBundle = null;
        String str = System.getProperty("user.dir") + File.separator + "config" + File.separator + paramString + ".properties";
        try(BufferedInputStream bufferedInputStream =new BufferedInputStream(new FileInputStream(str))) {
            propertyResourceBundle = new PropertyResourceBundle(bufferedInputStream);
            bufferedInputStream.close();
            if (propertyResourceBundle != null)
                logger.debug(" JAR方式部署，通过config目录读取配置：" + str);
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
        return propertyResourceBundle;
    }

    private static String db = "mysql";

    private static String diver_name = "com.mysql.cj.jdbc.Driver";

    private static String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8";

    private static String username = "root";

    private static String password = "root".trim();

    private static String database_name = "test";

    private static String project_path = "c:/workspace/test";

    private static String bussi_package = "com.csii";

    private static String source_root_package = "src";

    private static String webroot_package = "WebRoot";

    public static String template_path = "/auto-code/code-template/";

    private static boolean db_filed_convert = true;

    private static String db_table_id;

    private static String field_required_num = "4";

    private static String page_search_filed_num = "3";

    private static String page_filter_fields;

    private static String field_row_num = "1";

    public static String getDb() {
        return db;
    }

    public static String getField_required_num() {
        return field_required_num;
    }

    public static String getField_row_num() {
        return field_row_num;
    }

    static {
        diver_name = getDriverClass();
        url = getUrl();
        username = getUserName();
        password = getPassword();
        database_name = getDatabaseName();
        source_root_package = getSourceRootPackage();
        webroot_package = getWebrootPackage();
        bussi_package = getBassiPackage();
        template_path = getTemplatePath();
        project_path = getProjectPath();
        db_table_id = getDbTableId();
        db_filed_convert = getDbFiledConvert();
        page_filter_fields = getPageFilterFields();
        page_search_filed_num = getPageSearchFiledNum();
        if (url.indexOf("mysql") >= 0 || url.indexOf("MYSQL") >= 0) {
            db = "mysql";
        } else if (url.indexOf("oracle") >= 0 || url.indexOf("ORACLE") >= 0) {
            db = "oracle";
        } else if (url.indexOf("postgresql") >= 0 || url.indexOf("POSTGRESQL") >= 0) {
            db = "postgresql";
        } else if (url.indexOf("sqlserver") >= 0 || url.indexOf("SQLSERVER") >= 0) {
            db = "sqlserver";
        }
        source_root_package = source_root_package.replace(".", "/");
        webroot_package = webroot_package.replace(".", "/");
    }

    public static final String getDriverClass() {
        return database.getString("diver_name");
    }

    public static final String getUrl() {
        return database.getString("url");
    }

    public static final String getUserName() {
        return database.getString("username");
    }

    public static final String getPassword() {
        return database.getString("password");
    }

    public static final String getDatabaseName() {
        return database.getString("database_name");
    }

    public static final boolean getDbFiledConvert() {
        String str = config.getString("db_filed_convert");
        if (str.equals("false"))
            return false;
        return true;
    }

    public static String getBassiPackage() {
        return config.getString("bussi_package");
    }

    public static String getTemplatePath() {
        return config.getString("templatepath");
    }

    public static final String getSourceRootPackage() {
        return config.getString("source_root_package");
    }

    public static final String getWebrootPackage() {
        return config.getString("webroot_package");
    }

    public static final String getDbTableId() {
        return config.getString("db_table_id");
    }

    public static final String getPageFilterFields() {
        return config.getString("page_filter_fields");
    }

    public static final String getPageSearchFiledNum() {
        return config.getString("page_search_filed_num");
    }

    public static final String getPageFieldRequiredNum() {
        return config.getString("page_field_required_num");
    }

    public static String getProjectPath() {
        String str = config.getString("project_path");
        if (str != null && !"".equals(str))
            project_path = str;
        return project_path;
    }

    public static void setProjectPath(String paramString) {
        project_path = paramString;
    }

    public static void setTemplatePath(String paramString) {
        template_path = paramString;
    }
}
